//
//  HWAccountTool.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/11.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWAccountTool.h"
#import "HWAccount.h"
#define HWAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject] stringByAppendingPathComponent:@"account.archiver"]
@implementation HWAccountTool
+ (void)saveAccountWithAccount:(HWAccount *)account{
 
//    自定义对象的存储必须用nskeyedarchiver
        [NSKeyedArchiver archiveRootObject:account toFile:HWAccountPath];

}
/**
 *  返回账户信息
 *
 *  @return 账户模型（如果账户过期，返回nil）
 */
+ (HWAccount *)account{
  

    HWAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:HWAccountPath];
    //加载模型
       //验证账号是否过期
   long long expires_in =[account.expires_in longLongValue];
    //账号产生时间
    //过期时间
   NSDate *expires_time = [account.created_time dateByAddingTimeInterval:expires_in];
    //获得当前时间
    NSDate *now = [NSDate date];
    //去过
   NSComparisonResult result = [expires_time compare:now];
    /**
     *  NSOrderedAscending = -1L, 升序 右边大于左边
        NSOrderedSame,
        NSOrderedDescending 降序 右边 小于 左边
     */
    if (result != NSOrderedDescending) {//过期
        return nil;
    }
    return account;
}
@end
